import 'package:flutter/material.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/forms/profile.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerParent.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/sidebars/parentSidebar.dart';
import '../../themes/appTheme.dart';

class ParentProfile extends StatefulWidget {
  static const String routeName = '/parent/profile';

  @override
  State<ParentProfile> createState() => _ParentProfileState();
}

class _ParentProfileState extends State<ParentProfile> {
  final box = Hive.box('appconfig');
  final UserController _userController = Get.find();

  @override
  void initState() {
    bool isParent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Parent;
    if (isParent && _userController.profile.value.userName == null) {
      Future.delayed(Duration(milliseconds: 200), (() {
        _userController.getProfile();
      }));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build ParentProfile');

    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.parentPageItems
        .indexWhere((page) => page.route == Get.currentRoute);
    final PageStyles _pageStyles = PageStyles();

    return Scaffold(
        appBar: isSmallScreen ? CustomAppBar(title: 'Profile'.tr) : null,
        drawer: isSmallScreen ? ParentAppDrawer() : null,
        body: Container(
          decoration: _pageStyles.pageBoxDecoration(),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Visibility(
                visible: !isSmallScreen,
                child: ParentSidebarX(
                    selectedIndex: _selectedSidebarItem,
                    isBigScreen: isBigScreen),
              ),
              Expanded(
                child: Container(
                margin: _pageStyles.mainContainerPageEdgeInsets(),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(3.0),
                    color: Get.isDarkMode
                        ? AppTheme.colors.darkTheme1
                        : Colors.white.withOpacity(0.3)),
                child: Container(
                  margin: EdgeInsets.all(12.0),
                  child: SingleChildScrollView(
                    primary: false,
                    child: Center(
                      child: GetX<UserController>(
                          init: UserController(),
                          builder: (_userController) {
                            return Column(
                              children: [
                                ProfileWidget(
                                    profile: _userController.profile.value)
                              ],
                            );
                          }),
                    ),
                  ),
                ),
              )),
            ],
          ),
        ));
  }
}
